{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generates a new SDM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sdm as sdmlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "bits = 1000\n",
    "sample = 1000000\n",
    "scanner_type = sdmlib.SDM_SCANNER_OPENCL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate an address space with 1,000,000 random 1,000-bit bitstrings.\n",
    "address_space = sdmlib.AddressSpace.init_random(bits, sample)\n",
    "\n",
    "# Generate 1,000,000 counters initialized with value zero in the RAM memory.\n",
    "counter = sdmlib.Counter.init_zero(bits, sample)\n",
    "\n",
    "# Create a file to store the 1,000,000 counters initialized with value zero.\n",
    "# You do not need to provide file extension, because it will generate two files\n",
    "# and automatically included the extension for you.\n",
    "#counter = sdmlib.Counter.create_file('sdm-10w', bits, sample)\n",
    "\n",
    "# Create an SDM with the generated address space and counter.\n",
    "# The scans will be performed using the OpenCL scanner.\n",
    "sdm = sdmlib.SDM(address_space, counter, 451, scanner_type)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 2 3 4 5 6 7 8 9 \n",
      "10 bitstring wrote into memory.\n"
     ]
    }
   ],
   "source": [
    "v = []\n",
    "for i in xrange(10):\n",
    "    print i,\n",
    "    # Generate a random 1,000-bit bitstring.\n",
    "    b = sdmlib.Bitstring.init_random(1000)\n",
    "    \n",
    "    # Write the bitstring to the SDM.\n",
    "    sdm.write(b, b)\n",
    "    v.append(b)\n",
    "print ''\n",
    "print '{} bitstring wrote into memory.'.format(len(v))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Distance 134\n"
     ]
    }
   ],
   "source": [
    "# Copy the bitstring from v[0].\n",
    "# We have to make a copy because the flip_random_bits function changes the bitstring itself.\n",
    "b = sdmlib.Bitstring.init_from_bitstring(v[0])\n",
    "b.flip_random_bits(400)\n",
    "\n",
    "# Read the bitstring from the SDM and checks the distance from the retrieved bitstring and v[0].\n",
    "c = sdm.read(b)\n",
    "print 'Distance', c.distance_to(v[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Save the address space into the file 'sdm-10w.as'.\n",
    "# The recommended extension for an address space is '.as'.\n",
    "\n",
    "# Although we have used 10w as an indication of 10 writes to the memory, the\n",
    "# address space is not affected by the writes. It is just a reference to help us\n",
    "# remeber that this address space has been used together with the counters.\n",
    "#address_space.save('sdm-10w.as');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
